package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.AdvertiseDao;
import guda.shop.cms.entity.Advertise;
import guda.shop.cms.manager.AdspaceMng;
import guda.shop.cms.manager.AdvertiseMng;
import guda.shop.common.hibernate3.Updater;
import guda.shop.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdvertiseMngImpl
        implements AdvertiseMng {
    private AdspaceMng adspaceMng;
    private AdvertiseDao advertiseDao;

    @Transactional(readOnly = true)
    public Pagination getPage(Integer paramInteger, Boolean paramBoolean, int paramInt1, int paramInt2) {
        Pagination localPagination = this.advertiseDao.getPage(paramInteger, paramBoolean, paramInt1, paramInt2);
        return localPagination;
    }

    @Transactional(readOnly = true)
    public List<Advertise> getList(Integer paramInteger, Boolean paramBoolean) {
        return this.advertiseDao.getList(paramInteger, paramBoolean);
    }

    @Transactional(readOnly = true)
    public Advertise findById(Integer paramInteger) {
        Advertise localAdvertise = this.advertiseDao.findById(paramInteger);
        return localAdvertise;
    }

    public Advertise save(Advertise paramAdvertise, Integer paramInteger, Map<String, String> paramMap) {
        paramAdvertise.setAdspace(this.adspaceMng.findById(paramInteger));
        paramAdvertise.setAttr(paramMap);
        paramAdvertise.init();
        this.advertiseDao.save(paramAdvertise);
        return paramAdvertise;
    }

    public Advertise update(Advertise paramAdvertise, Integer paramInteger, Map<String, String> paramMap) {
        Updater localUpdater = new Updater(paramAdvertise);
        paramAdvertise = this.advertiseDao.updateByUpdater(localUpdater);
        paramAdvertise.setAdspace(this.adspaceMng.findById(paramInteger));
        paramAdvertise.getAttr().clear();
        paramAdvertise.getAttr().putAll(paramMap);
        return paramAdvertise;
    }

    public Advertise deleteById(Integer paramInteger) {
        Advertise localAdvertise = this.advertiseDao.deleteById(paramInteger);
        return localAdvertise;
    }

    public Advertise[] deleteByIds(Integer[] paramArrayOfInteger) {
        Advertise[] arrayOfAdvertise = new Advertise[paramArrayOfInteger.length];
        int i = 0;
        int j = paramArrayOfInteger.length;
        while (i < j) {
            arrayOfAdvertise[i] = deleteById(paramArrayOfInteger[i]);
            i++;
        }
        return arrayOfAdvertise;
    }

    public void display(Integer paramInteger) {
        Advertise localAdvertise = findById(paramInteger);
        if (localAdvertise != null)
            localAdvertise.setDisplayCount(Integer.valueOf(localAdvertise.getDisplayCount().intValue() + 1));
    }

    public void click(Integer paramInteger) {
        Advertise localAdvertise = findById(paramInteger);
        if (localAdvertise != null)
            localAdvertise.setClickCount(Integer.valueOf(localAdvertise.getClickCount().intValue() + 1));
    }

    @Autowired
    public void setAdspaceMng(AdspaceMng paramAdspaceMng) {
        this.adspaceMng = paramAdspaceMng;
    }

    @Autowired
    public void setDao(AdvertiseDao paramAdvertiseDao) {
        this.advertiseDao = paramAdvertiseDao;
    }
}

